//
//  HBTagWaterLayoutViewController.m
//  HBLibary_Example
//
//  Created by Rambo on 2018/6/19.
//  Copyright © 2018年 Rambo. All rights reserved.
//

#import "HBTagWaterLayoutViewController.h"
#import "HBTagWaterCollectionViewCell.h"
#import "HBTagWaterCollectionReusableView.h"
#import <HBLibary/HBLibary.h>

@interface HBTagWaterLayoutViewController ()<UICollectionViewDelegate, UICollectionViewDataSource, HBTagWaterLayoutDelegate>

@property (nonatomic, strong) UICollectionView *collectionView;

@property (nonatomic, strong) HBTagWaterLayout *collectionViewLayout;

@end

@implementation HBTagWaterLayoutViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUI];
    
    NSMutableArray *titles = [NSMutableArray array];
    for (NSInteger i = 0; i < 4; i++) {
        NSMutableArray *array = [NSMutableArray array];
        for (NSInteger j = 0; j < 20; j++) {
            NSString *title = [NSString stringWithFormat:@"标题%zd---%zd",i,j];
            [array addObject:title];
        }
        [titles addObject:array];
    }
    self.collectionViewLayout.titles = titles;
}

#pragma mark - UICollectionViewDataSource

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return self.collectionViewLayout.titles.count;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return [self.collectionViewLayout.titles[section] count];
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    HBTagWaterCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:HBTagWaterCollectionViewCellId forIndexPath:indexPath];
    if (self.collectionViewLayout.titles.count > indexPath.section &&
        [self.collectionViewLayout.titles[indexPath.section] count] > indexPath.item) {
        NSString *title = self.collectionViewLayout.titles[indexPath.section][indexPath.item];
        cell.title = title;
    }
    return cell;
}



#pragma mark - UICollectionViewDelegate

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    
    if ([kind isEqualToString:HBTagWaterLayoutHeaderViewKind]) {
        HBTagWaterCollectionReusableView *view = [collectionView dequeueReusableSupplementaryViewOfKind:HBTagWaterLayoutHeaderViewKind withReuseIdentifier:HBTagWaterLayoutHeaderViewKind forIndexPath:indexPath];
        view.backgroundColor = [UIColor whiteColor];
        view.title = @"一级域名";
        return view;
    }else if ([kind isEqualToString:HBTagWaterLayoutFooterViewKind]){
        UICollectionReusableView *view = [collectionView dequeueReusableSupplementaryViewOfKind:HBTagWaterLayoutFooterViewKind withReuseIdentifier:HBTagWaterLayoutFooterViewKind forIndexPath:indexPath];
        view.backgroundColor = [UIColor redColor];
        return view;
    }
    return nil;
}


#pragma mark - HBGroupWaterLayoutDelegate

/**
 组头大小
 
 @param collectionView collectionView
 @param collectionViewLayout collectionViewLayout
 @param section section
 @return 大小
 */
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(HBTagWaterLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    return CGSizeMake(0, 42);
}

/**
 组尾大小
 
 @param collectionView collectionView
 @param collectionViewLayout collectionViewLayout
 @param section section
 @return 大小
 */
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(HBTagWaterLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section{
    return CGSizeMake(0, 10);
}


#pragma mark - 初始化

- (void)setUI{
    UIView *superView = self.view;
    superView.backgroundColor = [UIColor whiteColor];
    self.collectionView = ({
        HBTagWaterLayout *layout = [[HBTagWaterLayout alloc] init];
        layout.delegate = self;
        layout.minimumLineSpacing = 12;
        layout.minimumInteritemSpacing = 12;
        layout.itemEdgeInsets = HBTagWaterCollectionViewCell.edgeInsets;
        layout.contentEdgeInsets = UIEdgeInsetsMake(22, 25, 22, 25);
        layout.font = HBTagWaterCollectionViewCell.titleFont;
        self.collectionViewLayout = layout;
        
        UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        collectionView.delegate = self;
        collectionView.dataSource = self;
        collectionView.backgroundColor = [UIColor whiteColor];
        [collectionView registerClass:[HBTagWaterCollectionViewCell class] forCellWithReuseIdentifier:HBTagWaterCollectionViewCellId];
        [collectionView registerClass:[HBTagWaterCollectionReusableView class] forSupplementaryViewOfKind:HBTagWaterLayoutHeaderViewKind withReuseIdentifier:HBTagWaterLayoutHeaderViewKind];
        [collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:HBTagWaterLayoutFooterViewKind withReuseIdentifier:HBTagWaterLayoutFooterViewKind];
        [superView addSubview:collectionView];
        collectionView.frame = self.view.bounds;
        
        collectionView;
    });
}



@end
